/**
 * @author chenz
 */
function websiteNote(obj) {
	if (!obj) {
		try {
			this.uuid = new String(new UUID());
			this.webLink = websitenotes.doc.URL;
			this.noteDiv = websitenotes.doc.createElement ("DIV");
			this.noteDiv.style.position = "absolute";
			this.noteDiv.style.top = websitenotes.posy + "px";
			this.noteDiv.style.left = websitenotes.posx + "px";
			this.noteDiv.style.height = "225px";
			this.noteDiv.style.width = "225px";
			this.noteDiv.style.backgroundImage = "url(chrome://websitenotes/content/note.png);"; //"yellow";
			this.noteDiv.style.zIndex = "10000";
			this.noteDiv.id = "note-"+this.uuid;
			websitenotes.doc.body.appendChild(this.noteDiv);
		
			this.headerDiv = websitenotes.doc.createElement ("DIV");
			this.headerDiv.style.position = "relative";
			this.headerDiv.style.left = "8px";
			this.headerDiv.style.top = "4px";
			this.headerDiv.style.height = "15px";
			this.headerDiv.style.width = "200px";
			this.headerDiv.style.backgroundRepeat = "no-repeat;";
			this.headerDiv.style.background = "url(chrome://websitenotes/content/move-light.png) no-repeat center;";
			this.headerDiv.style.zIndex = "10000";
			this.headerDiv.id = "header-"+this.uuid;
			this.noteDiv.appendChild(this.headerDiv);
			
			this.saveDiv = websitenotes.doc.createElement("DIV");
			this.saveDiv.style.position = "relative";
			this.saveDiv.style.left = "0px";
			this.saveDiv.style.top = "0px";
			this.saveDiv.style.height = "15px";
			this.saveDiv.style.width = "15px";
			this.saveDiv.style.backgroundImage = "url(chrome://websitenotes/content/saved.png);";
			this.saveDiv.style.zIndex = "10000";
			this.saveDiv.id = "save-"+this.uuid;
			this.saveDiv.uuid = this.uuid;
			this.headerDiv.appendChild(this.saveDiv);
			
			this.trashDiv = websitenotes.doc.createElement("DIV");
			this.trashDiv.style.position = "relative";
			this.trashDiv.style.left = "185px";
			this.trashDiv.style.top = "-15px"; //Huh? WTF wont this work as intended with "0px"?
			this.trashDiv.style.height = "15px";
			this.trashDiv.style.width = "15px";
			this.trashDiv.style.backgroundImage = "url(chrome://websitenotes/content/trash-light.png);";
			this.trashDiv.style.zIndex = "10000";
			this.trashDiv.id = "trash-"+this.uuid;
			this.trashDiv.uuid = this.uuid;
			this.headerDiv.appendChild(this.trashDiv);

			this.bodyDiv = websitenotes.doc.createElement("DIV");
			this.bodyDiv.style.width = "175px";
			this.bodyDiv.style.position = "relative";
			this.bodyDiv.style.left = "8px";
			this.bodyDiv.style.top = "4px";
 			this.bodyDiv.style.visibility = 'visible';
			this.bodyDiv.id = "body-"+this.uuid;
			this.noteDiv.appendChild(this.bodyDiv);
			
			this.formElm = websitenotes.doc.createElement("FORM");
			this.formElm.id = "form-"+this.uuid;
			this.formElm.name = "form-"+this.uuid;
			this.formElm.style.visibility = 'visible';
			this.bodyDiv.appendChild(this.formElm);
			
			this.textArea = websitenotes.doc.createElement("TEXTAREA");
			this.textArea.style.width = "200px";
			this.textArea.style.height = "175px";
			this.textArea.style.border = "none";
			this.textArea.style.background = "transparent";
			this.textArea.style.font = "12px arial";
			this.textArea.style.visibility = 'visible';
			this.textArea.id = "textarea-"+this.uuid;
			this.formElm.appendChild(this.textArea);
		} catch(err) {
			alert(err + "\n\n" + this);
		}
	}
	else if (obj) {
		try {
			this.uuid = obj.uuid;
			this.webLink = obj.webLink;
			
			this.noteDiv = websitenotes.doc.createElement ("DIV");
			this.noteDiv.style.position = obj.noteDiv.style.position;
			this.noteDiv.style.top = obj.noteDiv.style.top;
			this.noteDiv.style.left = obj.noteDiv.style.left;
			this.noteDiv.style.height = obj.noteDiv.style.height;
			this.noteDiv.style.width = obj.noteDiv.style.width;
			this.noteDiv.style.backgroundImage = "url(chrome://websitenotes/content/note.png);"; //obj.noteDiv.style.background;
			this.noteDiv.style.zIndex = obj.noteDiv.style.zIndex;
			this.noteDiv.id = obj.noteDiv.id
			websitenotes.doc.body.appendChild(this.noteDiv);
		
			this.headerDiv = websitenotes.doc.createElement ("DIV");
			this.headerDiv.style.position = "relative";
			this.headerDiv.style.left = "8px";
			this.headerDiv.style.top = "4px";
			this.headerDiv.style.height = obj.headerDiv.style.height;
			this.headerDiv.style.width = obj.headerDiv.style.width;
			this.headerDiv.style.backgroundRepeat = "no-repeat;";
			this.headerDiv.style.background = "url(chrome://websitenotes/content/move-light.png) no-repeat center;";
			this.headerDiv.style.zIndex = obj.headerDiv.style.zIndex;
			this.headerDiv.id = obj.headerDiv.id;
			this.noteDiv.appendChild(this.headerDiv);
		
		  this.saveDiv = websitenotes.doc.createElement("DIV");
			this.saveDiv.style.position = obj.saveDiv.style.position;
			//this.saveDiv.style.display = obj.saveDiv.style.display;
			this.saveDiv.style.left = obj.saveDiv.style.left;
			this.saveDiv.style.top = obj.saveDiv.style.top;
			this.saveDiv.style.height = obj.saveDiv.style.height;
			this.saveDiv.style.width = obj.saveDiv.style.width;
			this.saveDiv.style.backgroundImage = "url(chrome://websitenotes/content/saved.png);";
			this.saveDiv.style.zIndex = obj.saveDiv.style.zIndex;
			this.saveDiv.id = obj.saveDiv.id;
			this.saveDiv.uuid = obj.saveDiv.uuid;
			this.headerDiv.appendChild(this.saveDiv);
		
		  this.trashDiv = websitenotes.doc.createElement("DIV");
			this.trashDiv.style.position = obj.trashDiv.style.position;
			//this.trashDiv.style.display = obj.trashDiv.style.display;
			this.trashDiv.style.left = obj.trashDiv.style.left;
			this.trashDiv.style.top = obj.trashDiv.style.top;
			this.trashDiv.style.height = obj.trashDiv.style.height;
			this.trashDiv.style.width = obj.trashDiv.style.width;
			this.trashDiv.style.backgroundImage = "url(chrome://websitenotes/content/trash-light.png);";
			this.trashDiv.style.zIndex = obj.trashDiv.style.zIndex;
			this.trashDiv.id = obj.trashDiv.id;
			this.trashDiv.uuid = obj.trashDiv.uuid;
			this.headerDiv.appendChild(this.trashDiv);
			
			this.bodyDiv = websitenotes.doc.createElement("DIV");
			this.bodyDiv.style.width = obj.bodyDiv.style.width;
 			this.bodyDiv.style.visibility = obj.bodyDiv.style.visibility;
			this.bodyDiv.style.position = "relative";
			this.bodyDiv.style.left = "8px";
			this.bodyDiv.style.top = "4px";
			this.bodyDiv.id = obj.bodyDiv.id;
			this.noteDiv.appendChild(this.bodyDiv);
			
			this.formElm = websitenotes.doc.createElement("FORM");
			this.formElm.id = obj.formElm.id;
			this.formElm.name = obj.formElm.name;
			this.formElm.style.visibility = obj.formElm.style.visibility;
			this.bodyDiv.appendChild(this.formElm);
			
			this.textArea = websitenotes.doc.createElement("TEXTAREA");
			this.textArea.style.width = obj.textArea.style.width;
			this.textArea.style.height = obj.textArea.style.height;
			this.textArea.style.border = obj.textArea.style.border;
			this.textArea.style.background = obj.textArea.style.background;
			this.textArea.style.font = obj.textArea.style.font;
			this.textArea.style.visibility = obj.textArea.style.visibility;
			this.textArea.id = obj.textArea.id;
			this.formElm.appendChild(this.textArea);
		} catch(err) {
			alert(err + "\n\n" + this);
		}
	}
	this.trashDiv.addEventListener("click", 
				function(e) {
					try {
						//websitenotes.noteHashArray.trashNote(e.currentTarget.uuid);
						websitenotes.doc.body.removeChild(websitenotes.doc.getElementById("note-"+e.currentTarget.uuid));
						websitenotes.removeNoteFromLocalDatabase(e.currentTarget.uuid);
					}
					catch (e) {
						alert(e);
					}
				}, true);
	this.saveDiv.addEventListener("click", 
				function(e) {
					//alert("Made it here.");
					try {
						var note = websitenotes.doc.getElementById("note-"+e.currentTarget.uuid);
						var textArea = websitenotes.doc.getElementById("textarea-"+e.currentTarget.uuid);
						websitenotes.setNoteTextInLocalDatabase(e.currentTarget.uuid, textArea.value);
						//websitenotes.getNoteFromLocalDatabase(e.currentTarget.uuid);
						//alert("saveDiv.click sees noteText as \""+textArea.value+"\" for textarea-"+e.currentTarget.uuid);
					}
					catch (e) {
						alert(e);
					}
				}, true);
	this.toObject = function (note) { return convertNoteToObject(note); };
  return this;
}

function convertNoteToObject(note) {
	
	var newObj = new Object();
	newObj.uuid = note.uuid;
	newObj.webLink = note.webLink;
	
	newObj.noteDiv = new Object(); //Assumed noteDiv is a DIV element
	newObj.noteDiv.style = new Object();
	newObj.noteDiv.style.position = note.noteDiv.style.position;
	newObj.noteDiv.style.top = note.noteDiv.style.top;
	newObj.noteDiv.style.left = note.noteDiv.style.left;
	newObj.noteDiv.style.height = note.noteDiv.style.height;
	newObj.noteDiv.style.width = note.noteDiv.style.width;
	newObj.noteDiv.style.backgroundImage = "url(chrome://websitenotes/content/note.png)"; //note.noteDiv.style.background;
	newObj.noteDiv.style.zIndex = note.noteDiv.style.zIndex;
	newObj.noteDiv.id = note.noteDiv.id

  newObj.headerDiv = new Object();
	newObj.headerDiv.style = new Object();
	newObj.headerDiv.style.left = note.headerDiv.style.left;
	newObj.headerDiv.style.top = note.headerDiv.style.top;
	newObj.headerDiv.style.height = note.headerDiv.style.height;
	newObj.headerDiv.style.width = note.headerDiv.style.width;
	newObj.headerDiv.style.background = note.headerDiv.style.background;
	newObj.headerDiv.style.zIndex = note.headerDiv.style.zIndex;
	newObj.headerDiv.id = note.headerDiv.id
	
	newObj.trashDiv = new Object();
	newObj.trashDiv.style = new Object();
	newObj.trashDiv.style.position = note.trashDiv.style.position;
	//newObj.trashDiv.style.display = note.trashDiv.style.display;
	newObj.trashDiv.style.left = note.trashDiv.style.left;
	newObj.trashDiv.style.top = note.trashDiv.style.top;
	newObj.trashDiv.style.height = note.trashDiv.style.height;
	newObj.trashDiv.style.width = note.trashDiv.style.width;
	newObj.trashDiv.style.backgroundColor = note.trashDiv.style.backgroundColor;
	newObj.trashDiv.style.zIndex = note.trashDiv.style.zIndex;
	newObj.trashDiv.id = note.trashDiv.id;
	newObj.trashDiv.uuid = note.trashDiv.uuid;
	
	newObj.saveDiv = new Object();
	newObj.saveDiv.style = new Object();
	newObj.saveDiv.style.position = note.saveDiv.style.position;
	newObj.saveDiv.style.display = note.saveDiv.style.display;
	newObj.saveDiv.style.left = note.saveDiv.style.left;
	newObj.saveDiv.style.top = note.saveDiv.style.top;
	newObj.saveDiv.style.height = note.saveDiv.style.height;
	newObj.saveDiv.style.width = note.saveDiv.style.width;
	newObj.saveDiv.style.backgroundColor = note.saveDiv.style.backgroundColor;
	newObj.saveDiv.style.zIndex = note.saveDiv.style.zIndex;
	newObj.saveDiv.id = note.saveDiv.id;
	newObj.saveDiv.uuid = note.saveDiv.uuid;
	
	newObj.bodyDiv = new Object();
	newObj.bodyDiv.style = new Object();
 	newObj.bodyDiv.style.width = note.bodyDiv.style.width;
 	newObj.bodyDiv.style.visibility = note.bodyDiv.style.visibility;
	newObj.bodyDiv.id = note.bodyDiv.id;
	
	newObj.formElm = new Object();
	newObj.formElm.id = note.formElm.id;
	newObj.formElm.name = note.formElm.name;
	newObj.formElm.style = new Object();
	newObj.formElm.style.visibility = note.formElm.style.visibility;
		
	newObj.textArea = new Object();
	newObj.textArea.id = note.textArea.id;
	newObj.textArea.style = new Object();
	newObj.textArea.style.width = note.textArea.style.width;
	newObj.textArea.style.height = note.textArea.style.height;
	newObj.textArea.style.border = note.textArea.style.border;
	newObj.textArea.style.background = note.textArea.style.background;
	newObj.textArea.style.font = note.textArea.style.font;
	newObj.textArea.style.visibility = note.textArea.style.visibility;
	newObj.textArea.id = note.textArea.id;

	return newObj;
}